package com.zhiying.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zhiying.config.StatusEnum;
import com.zhiying.domain.CMS_User;
import com.zhiying.domain.Role;
import com.zhiying.domain.RoleMenuVO;
import com.zhiying.mybatis.UserMapper;
import com.zhiying.page.BasePageDTO;
import com.zhiying.page.CmsPageResult;
import com.zhiying.page.CmsResult;
import com.zhiying.page.PageInfo;
import com.zhiying.page.RoleMenuDTO;
import com.zhiying.page.UserPageDTO;
import com.zhiying.service.IRoleMenuService;
import com.zhiying.service.IRoleService;
import com.zhiying.service.LoginService;
import com.zhiying.service.MenuService;
import com.zhiying.service.RoleService;
import com.zhiying.until.code.CommonReturnCode;


/**
 * 
 * 项目名称：morning-cms-web Maven Webapp   
 * 类名称：AdministratorRoleController   
 * 类描述：角色管理表示层控制器        
 * 创建人：陈星星   
 * 创建时间：2017年4月6日 下午11:32:34   
 *
 */
@Controller
@RequestMapping(value = "/administrator/role")
public class AdministratorRoleController  {

	@Autowired
	private RoleService roleService;

	@Autowired
	private IRoleMenuService iRoleMenuService;

	@Autowired
	private LoginService loginService;

	@Autowired
	private IRoleService iRoleService;
	
	@Autowired
	MenuService  menuService;

	@Autowired
	UserMapper userMapper;

	/**
	 * GET 角色列表页面
	 * @param model
	 * @return
	 */
	@RequiresPermissions("administrator:role:view")
	@GetMapping(value = "/view")
	public String getRolePage(Model model) {
		return "/role/admin_role_list";
	}

	/**
	 * GET 角色列表
	 * @param pageInfo
	 * @param search
	 * @return
	 */
	@RequiresPermissions("administrator:role:view")
	@GetMapping(value = "/")
	@ResponseBody
	public Object listRole(PageInfo pageInfo, @RequestParam(required = false, value = "search") String search) {
		BasePageDTO<Role> basePageDTO = roleService.selectUserRoleInfo(pageInfo, search);
		return new CmsPageResult(basePageDTO.getList(), basePageDTO.getPageInfo().getTotal());
	}

	/**
	 * PUT 启用/冻结角色
	 * @param roleId 角色ID
	 * @return
	 */
	@RequiresPermissions("administrator:role:audit")
	@PutMapping(value = "/{roleId}/audit")
	@ResponseBody
	public Object audit(@PathVariable("roleId") Long roleId) {
		/*AuthorizingUser authorizingUser = SingletonLoginUtils.getUser();
		if (authorizingUser != null) {*/
		Role role=new Role();
		role.setRoleId(roleId);
		Integer count = roleService.updatestatusByid(role);
		return new CmsResult(CommonReturnCode.SUCCESS, count);
		/*
		 * } else { return new CmsResult(CommonReturnCode.UNAUTHORIZED); }
		 */
	}
	/**
	 * DELETE 删除角色
	 * @param roleId 角色ID
	 * @return
	 */
	@RequiresPermissions("administrator:role:delete")
	@DeleteMapping(value = "/{roleId}")
	@ResponseBody
	public Object delete(@PathVariable("roleId") int roleId) {
		//		AuthorizingUser authorizingUser = SingletonLoginUtils.getUser();
		//		if (authorizingUser != null) {
		Integer User_Role_count=roleService.selectCmsRoleByRoleId(roleId);
		if(User_Role_count>0) {
			return new CmsResult(CommonReturnCode.Fail, 7887);
		}else {
			Integer count = roleService.deleteByRoleId(roleId);
			return new CmsResult(CommonReturnCode.SUCCESS, count);
		}
		//Integer count = roleService.deleteByRoleId(roleId);

		//		} else {
		//			return new CmsResult(CommonReturnCode.UNAUTHORIZED);
		//		}
	}

	/**
	 * GET 更新角色页面
	 * @return
	 */
	@RequiresPermissions("administrator:role:edit")
	@GetMapping(value = "/{roleId}/edit")
	public String getUpdatePage(Model model, @PathVariable("roleId") Long roleId) {
		// 目录是否选中
		List<RoleMenuVO> menus = iRoleMenuService.listCheckedMenus(roleId, StatusEnum.SHOW.getStatus());
		model.addAttribute("menus", JSON.toJSON(menus));

		// 角色信息
		Role role = roleService.selectRoleInfoById(roleId);
		model.addAttribute("role", role);

		return "/role/admin_role_update";
	}
	/**
	 * PUT 更新角色信息
	 * @return
	 */
	@RequiresPermissions("administrator:role:edit")
	@PutMapping(value = "/{roleId}")
	@ResponseBody
	public Object update(Role role, @PathVariable("roleId") Long roleId,
			@RequestParam(required = false, value = "menuIds") String menuId) {
		String[] menuIds = menuId.split(",");

		//			AuthorizingUser authorizingUser = SingletonLoginUtils.getUser();
		//			if (authorizingUser != null) {
		// 更新用户及角色记录
		CMS_User user=loginService.getUserInfoByUserName((String)SecurityUtils.getSubject().getSession().getAttribute("loginName"));
		Integer count = iRoleService.updateRole(role, menuIds, user.getUserName());
		return new CmsResult(CommonReturnCode.SUCCESS, count);
		//			} else {
		//				return new CmsResult(CommonReturnCode.UNAUTHORIZED);
		//			}
	}


	/**
	 * GET 角色分类下管理员列表
	 * @return
	 */
	@RequiresPermissions("administrator:list:view")
	@GetMapping(value = "/{roleId}/lists")
	@ResponseBody
	public Object listLogs(@PathVariable("roleId") int roleId, PageInfo pageInfo,
			@RequestParam(required = false, value = "search") String search) {
		//				AuthorizingUser authorizingUser = SingletonLoginUtils.getUser();
		//		
		//				if (authorizingUser != null) {
		System.out.println("roleId=============>"+roleId);
		// 用户日志
		UserPageDTO userPageDTO = roleService.listByRoleId(roleId, pageInfo, search);
		return new CmsPageResult(userPageDTO.getUserVOs(),userPageDTO.getPageInfo().getTotal());
		//				} else {
		//					return new CmsResult(CommonReturnCode.UNAUTHORIZED);
		//				}
	}

	/**
	 * GET 角色分类下管理员列表页面
	 * @param roleId
	 * @return
	 */
	@RequiresPermissions("administrator:role:view")
	@GetMapping(value = "/{roleId}/list")
	public String list(Model model, @PathVariable("roleId") Long roleId) {
		model.addAttribute("roleId", roleId);
		return "/role/admin_user_role";
	}

	@RequiresPermissions("administrator:role:create")
	@GetMapping(value = "/create")
	public String getInsertPage(Model model) {
		List<RoleMenuVO> menus = iRoleMenuService.listCreatMenus(Long.valueOf(0),StatusEnum.SHOW.getStatus());
		model.addAttribute("menus", JSON.toJSON(menus));
		return "/role/admin_role_create";
	}

	/**
	 * POST 创建角色
	 * @return
	 */
	@RequiresPermissions("administrator:role:create")
	@PostMapping(value = "")
	@ResponseBody
	public Object insert(Role role, @RequestParam(required = false, value = "menuIds") String menuId) {

		String[] menuIds = menuId.split(",");

//		AuthorizingUser authorizingUser = SingletonLoginUtils.getUser();
//		if (authorizingUser != null) {
			// 创建角色及插入角色目录记录
		CMS_User user=loginService.getUserInfoByUserName((String)SecurityUtils.getSubject().getSession().getAttribute("loginName"));	
		Integer count = iRoleService.createRole(role, menuIds, user.getUserName());
			return new CmsResult(CommonReturnCode.SUCCESS, count);
//		} else {
//			return new CmsResult(CommonReturnCode.UNAUTHORIZED);
//		}
	}
	/**
	 * GET 个人资料
	 * @return
	 */
	@RequiresPermissions("administrator:list:view")
	@GetMapping(value = "/{userId}")
	public String detail(Model model, @PathVariable  Long userId){
		// 用户信息
		CMS_User user = menuService.selectUserById(userId);
		model.addAttribute("user", user);
		
		// 分配角色
		List<Role> roles = userMapper.listByUserIds(StatusEnum.NORMAL.getStatus(), user.getUserId());
		model.addAttribute("roles", roles);// 用户权限
		
		return "/admin/admin_user_message";
	}
	//	/**
	//	 * GET 角色分类下管理员列表页面
	//	 * @param roleId
	//	 * @return
	//	 */
	//	@ApiOperation(value = "角色分类下管理员列表页面", notes = "角色分类下管理员列表页面")
	//	@RequiresPermissions("administrator:role:view")
	//	@GetMapping(value = "/{roleId}/list")
	//	public String list(Model model, @PathVariable("roleId") Long roleId) {
	//		model.addAttribute("roleId", roleId);
	//		return "/modules/role/admin_user_role";
	//	}
	//	
	//	/**
	//	 * GET 角色分类下管理员列表
	//	 * @return
	//	 */
	//	@ApiOperation(value = "角色分类下管理员列表", notes = "根据分页信息/搜索内容查询角色分类下管理员列表")
	//	@RequiresPermissions("administrator:list:view")
	//	@GetMapping(value = "/{roleId}/lists")
	//	@ResponseBody
	//	public Object listLogs(@PathVariable("roleId") Long roleId, PageInfo pageInfo,
	//			@RequestParam(required = false, value = "search") String search) {
	//		AuthorizingUser authorizingUser = SingletonLoginUtils.getUser();
	//
	//		if (authorizingUser != null) {
	//			// 用户日志
	//			UserPageDTO userPageDTO = userRoleService.listByRoleId(roleId, pageInfo, search);
	//			return new CmsPageResult(userPageDTO.getUserVOs(),
	//					userPageDTO.getPageInfo().getTotal());
	//		} else {
	//			return new CmsResult(CommonReturnCode.UNAUTHORIZED);
	//		}
	//	}
	//	
	//	/**
	//	 * PUT 启用/冻结角色
	//	 * @param roleId 角色ID
	//	 * @return
	//	 */
	//	@ApiOperation(value = "启用/冻结角色", notes = "根据url角色ID启动/冻结角色")
	//	@RequiresPermissions("administrator:role:audit")
	//	@PutMapping(value = "/{roleId}/audit")
	//	@ResponseBody
	//	public Object audit(@PathVariable("roleId") Long roleId) {
	//		AuthorizingUser authorizingUser = SingletonLoginUtils.getUser();
	//		if (authorizingUser != null) {
	//			Integer count = roleService.updateStatus(roleId);
	//			return new CmsResult(CommonReturnCode.SUCCESS, count);
	//		} else {
	//			return new CmsResult(CommonReturnCode.UNAUTHORIZED);
	//		}
	//	}
	//	
	//	/**
	//	 * DELETE 删除角色
	//	 * @param roleId 角色ID
	//	 * @return
	//	 */
	//	@ApiOperation(value = "删除角色", notes = "根据url角色ID删除角色")
	//	@RequiresPermissions("administrator:role:delete")
	//	@DeleteMapping(value = "/{roleId}")
	//	@ResponseBody
	//	public Object delete(@PathVariable("roleId") Long roleId) {
	//		AuthorizingUser authorizingUser = SingletonLoginUtils.getUser();
	//		if (authorizingUser != null) {
	//			Integer count = roleService.deleteByRoleId(roleId);
	//			return new CmsResult(CommonReturnCode.SUCCESS, count);
	//		} else {
	//			return new CmsResult(CommonReturnCode.UNAUTHORIZED);
	//		}
	//	}
	//	
	//	/**
	//	 * GET 创建角色页面
	//	 * @return
	//	 */
	//	@ApiOperation(value = "创建角色页面", notes = "创建角色页面")
	//	@RequiresPermissions("administrator:role:create")
	//	@GetMapping(value = "/create")
	//	public String getInsertPage(Model model) {
	//		List<RoleMenuDTO> menus = roleMenuService.listRoleMenus(StatusEnum.SHOW.getStatus());
	//		model.addAttribute("menus", JSON.toJSON(menus));
	//		return "/modules/role/admin_role_create";
	//	}
	//	
	//	/**
	//	 * POST 创建角色
	//	 * @return
	//	 */
	//	@ApiOperation(value = "创建角色", notes = "创建角色")
	//	@RequiresPermissions("administrator:role:create")
	//	@PostMapping(value = "")
	//	@ResponseBody
	//	public Object insert(Role role, @RequestParam(required = false, value = "menuIds") String menuId) {
	//
	//		String[] menuIds = menuId.split(",");
	//
	//		AuthorizingUser authorizingUser = SingletonLoginUtils.getUser();
	//		if (authorizingUser != null) {
	//			// 创建角色及插入角色目录记录
	//			Integer count = roleService.insertRole(role, menuIds, authorizingUser.getUserName());
	//			return new CmsResult(CommonReturnCode.SUCCESS, count);
	//		} else {
	//			return new CmsResult(CommonReturnCode.UNAUTHORIZED);
	//		}
	//	}
	//	
	//	/**
	//	 * GET 更新角色页面
	//	 * @return
	//	 */
	//	@ApiOperation(value = "更新角色页面", notes = "更新角色页面")
	//	@RequiresPermissions("administrator:role:edit")
	//	@GetMapping(value = "/{roleId}/edit")
	//	public String getUpdatePage(Model model, @PathVariable("roleId") Long roleId) {
	//		// 目录是否选中
	//		List<RoleMenuVO> menus = roleMenuService.listCheckedMenus(roleId, StatusEnum.SHOW.getStatus());
	//		model.addAttribute("menus", JSON.toJSON(menus));
	//		
	//		// 角色信息
	//		Role role = roleService.selectById(roleId);
	//		model.addAttribute("role", role);
	//		
	//		return "/modules/role/admin_role_update";
	//	}
	//	
	//	/**
	//	 * PUT 更新角色信息
	//	 * @return
	//	 */
	//	@ApiOperation(value = "更新角色信息", notes = "根据url角色ID来指定更新对象,并根据传过来的角色信息来更新角色信息")
	//	@RequiresPermissions("administrator:role:edit")
	//	@PutMapping(value = "/{roleId}")
	//	@ResponseBody
	//	public Object update(Role role, @PathVariable("roleId") Long roleId,
	//			@RequestParam(required = false, value = "menuIds") String menuId) {
	//		String[] menuIds = menuId.split(",");
	//		
	//		AuthorizingUser authorizingUser = SingletonLoginUtils.getUser();
	//		if (authorizingUser != null) {
	//			// 更新用户及角色记录
	//			Integer count = roleService.updateRole(role, menuIds, authorizingUser.getUserName());
	//			return new CmsResult(CommonReturnCode.SUCCESS, count);
	//		} else {
	//			return new CmsResult(CommonReturnCode.UNAUTHORIZED);
	//		}
	//	}
}
